<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

/**
 * 微信消息控制器
 * @package App\Http\Controllers\Admin
 */
class WxmsgController extends BaseController
{
    /**
     * 服务号消息管理
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function text(Request $request)
    {
        if ($request->ajax()) {
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\WxmsgText::select('*');
            //关键字（太消耗性能前端隐藏）
            $keyword = $request->get('keyword', '');
            if ($keyword) {
                $query->where('title', 'like', '%' . $keyword . '%');
            }
            //用户openId
            $openid = $request->get('openid', '');
            if ($openid) {
                $query->where('from_user_name', $openid);
            }
            //开始结束时间
            $startTime = $request->get('start_time', '');
            $endTime = $request->get('end_time', '');
            if (!empty($startTime) && !empty($endTime)) {
                $query->whereBetween('create_time', [$startTime, $endTime]);
            } elseif (!empty($startTime) && empty($endTime)) {
                $query->whereRaw("create_time >= '" . date('Y-m-d H:i:s', strtotime($startTime)) . "'");
            } elseif (empty($startTime) && !empty($endTime)) {
                $query->whereRaw("create_time <= '" . date('Y-m-d H:i:s', strtotime($endTime)) . "'");
            }
            return $query->orderBy('id', 'DESC')->paginate($pageSize);
        }
        return view('admin.wxmsg.text', $this->viewData);
    }

    /**
     * 图片消息
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function image(Request $request)
    {
        if ($request->ajax()) {
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\WxmsgImage::select('*');
            //用户openId
            $openid = $request->get('openid', '');
            if ($openid) {
                $query->where('from_user_name', $openid);
            }
            //开始结束时间
            $startTime = $request->get('start_time', '');
            $endTime = $request->get('end_time', '');
            if (!empty($startTime) && !empty($endTime)) {
                $query->whereBetween('create_time', [$startTime, $endTime]);
            } elseif (!empty($startTime) && empty($endTime)) {
                $query->whereRaw("create_time >= '" . date('Y-m-d H:i:s', strtotime($startTime)) . "'");
            } elseif (empty($startTime) && !empty($endTime)) {
                $query->whereRaw("create_time <= '" . date('Y-m-d H:i:s', strtotime($endTime)) . "'");
            }
            return $query->orderBy('id', 'DESC')->paginate($pageSize);
        }
        return view('admin.wxmsg.image', $this->viewData);
    }

    /**
     * 语音消息
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function voice(Request $request)
    {
        if ($request->ajax()) {
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\WxmsgVoice::select('*');
            //用户openId
            $openid = $request->get('openid', '');
            if ($openid) {
                $query->where('from_user_name', $openid);
            }
            //开始结束时间
            $startTime = $request->get('start_time', '');
            $endTime = $request->get('end_time', '');
            if (!empty($startTime) && !empty($endTime)) {
                $query->whereBetween('create_time', [$startTime, $endTime]);
            } elseif (!empty($startTime) && empty($endTime)) {
                $query->whereRaw("create_time >= '" . date('Y-m-d H:i:s', strtotime($startTime)) . "'");
            } elseif (empty($startTime) && !empty($endTime)) {
                $query->whereRaw("create_time <= '" . date('Y-m-d H:i:s', strtotime($endTime)) . "'");
            }
            return $query->orderBy('id', 'DESC')->paginate($pageSize);
        }
        return view('admin.wxmsg.voice', $this->viewData);
    }

    /**
     * 视频消息
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function video(Request $request)
    {
        if ($request->ajax()) {
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\WxmsgVideo::select('*');
            //用户openId
            $openid = $request->get('openid', '');
            if ($openid) {
                $query->where('from_user_name', $openid);
            }
            //开始结束时间
            $startTime = $request->get('start_time', '');
            $endTime = $request->get('end_time', '');
            if (!empty($startTime) && !empty($endTime)) {
                $query->whereBetween('create_time', [$startTime, $endTime]);
            } elseif (!empty($startTime) && empty($endTime)) {
                $query->whereRaw("create_time >= '" . date('Y-m-d H:i:s', strtotime($startTime)) . "'");
            } elseif (empty($startTime) && !empty($endTime)) {
                $query->whereRaw("create_time <= '" . date('Y-m-d H:i:s', strtotime($endTime)) . "'");
            }
            return $query->orderBy('id', 'DESC')->paginate($pageSize);
        }
        return view('admin.wxmsg.video', $this->viewData);
    }

    /**
     * 短视频消息
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function shortvideo(Request $request)
    {
        if ($request->ajax()) {
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\WxmsgShortvideo::select('*');
            //用户openId
            $openid = $request->get('openid', '');
            if ($openid) {
                $query->where('from_user_name', $openid);
            }
            //开始结束时间
            $startTime = $request->get('start_time', '');
            $endTime = $request->get('end_time', '');
            if (!empty($startTime) && !empty($endTime)) {
                $query->whereBetween('create_time', [$startTime, $endTime]);
            } elseif (!empty($startTime) && empty($endTime)) {
                $query->whereRaw("create_time >= '" . date('Y-m-d H:i:s', strtotime($startTime)) . "'");
            } elseif (empty($startTime) && !empty($endTime)) {
                $query->whereRaw("create_time <= '" . date('Y-m-d H:i:s', strtotime($endTime)) . "'");
            }
            return $query->orderBy('id', 'DESC')->paginate($pageSize);
        }
        return view('admin.wxmsg.shortvideo', $this->viewData);
    }

    /**
     * 位置消息
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function location(Request $request)
    {
        if ($request->ajax()) {
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\WxmsgLocation::select('*');
            //用户openId
            $openid = $request->get('openid', '');
            if ($openid) {
                $query->where('from_user_name', $openid);
            }
            //开始结束时间
            $startTime = $request->get('start_time', '');
            $endTime = $request->get('end_time', '');
            if (!empty($startTime) && !empty($endTime)) {
                $query->whereBetween('create_time', [$startTime, $endTime]);
            } elseif (!empty($startTime) && empty($endTime)) {
                $query->whereRaw("create_time >= '" . date('Y-m-d H:i:s', strtotime($startTime)) . "'");
            } elseif (empty($startTime) && !empty($endTime)) {
                $query->whereRaw("create_time <= '" . date('Y-m-d H:i:s', strtotime($endTime)) . "'");
            }
            return $query->orderBy('id', 'DESC')->paginate($pageSize);
        }
        return view('admin.wxmsg.location', $this->viewData);
    }

    /**
     * 链接消息
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function link(Request $request)
    {
        if ($request->ajax()) {
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\WxmsgLink::select('*');
            //用户openId
            $openid = $request->get('openid', '');
            if ($openid) {
                $query->where('from_user_name', $openid);
            }
            //开始结束时间
            $startTime = $request->get('start_time', '');
            $endTime = $request->get('end_time', '');
            if (!empty($startTime) && !empty($endTime)) {
                $query->whereBetween('create_time', [$startTime, $endTime]);
            } elseif (!empty($startTime) && empty($endTime)) {
                $query->whereRaw("create_time >= '" . date('Y-m-d H:i:s', strtotime($startTime)) . "'");
            } elseif (empty($startTime) && !empty($endTime)) {
                $query->whereRaw("create_time <= '" . date('Y-m-d H:i:s', strtotime($endTime)) . "'");
            }
            return $query->orderBy('id', 'DESC')->paginate($pageSize);
        }
        return view('admin.wxmsg.link', $this->viewData);
    }

    /**
     * 事件消息
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function event(Request $request)
    {
        $wxmsgSer = new \App\Services\WxmsgService();
        if ($request->ajax()) {
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\WxmsgEvent::select('*');
            //用户openId
            $openid = $request->get('openid', '');
            if ($openid) {
                $query->where('from_user_name', $openid);
            }
            //事件类型
            $event = $request->get('event', '');
            if ($event) {
                $query->where('event', $event);
            }
            //开始结束时间
            $startTime = $request->get('start_time', '');
            $endTime = $request->get('end_time', '');
            if (!empty($startTime) && !empty($endTime)) {
                $query->whereBetween('create_time', [$startTime, $endTime]);
            } elseif (!empty($startTime) && empty($endTime)) {
                $query->whereRaw("create_time >= '" . date('Y-m-d H:i:s', strtotime($startTime)) . "'");
            } elseif (empty($startTime) && !empty($endTime)) {
                $query->whereRaw("create_time <= '" . date('Y-m-d H:i:s', strtotime($endTime)) . "'");
            }
            $data = $query->orderBy('id', 'DESC')->paginate($pageSize);
            $data = $data->toArray();
            if ($data['data']) {
                $list = $data['data'];
                foreach ($list as $k => $v) {
                    $v['event_str'] = $wxmsgSer->getEventName($v['event']);
                    $v['data_str'] = '<pre>' . var_export($v['data'], true) . '</pre>';
                    $list[$k] = $v;
                }
                $data['data'] = $list;
            }
            return $data;
        }
        $this->viewData['eventList'] = $wxmsgSer->getEventList();
        return view('admin.wxmsg.event', $this->viewData);
    }

    /**
     * 被动回复消息
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function send(Request $request)
    {
        if ($request->ajax()) {
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\WxmsgSend::select('*');
            //用户openId
            $openid = $request->get('openid', '');
            if ($openid) {
                $query->where('to_user_name', $openid);
            }
            //开始结束时间
            $startTime = $request->get('start_time', '');
            $endTime = $request->get('end_time', '');
            if (!empty($startTime) && !empty($endTime)) {
                $query->whereBetween('create_time', [$startTime, $endTime]);
            } elseif (!empty($startTime) && empty($endTime)) {
                $query->whereRaw("create_time >= '" . date('Y-m-d H:i:s', strtotime($startTime)) . "'");
            } elseif (empty($startTime) && !empty($endTime)) {
                $query->whereRaw("create_time <= '" . date('Y-m-d H:i:s', strtotime($endTime)) . "'");
            }
            $data = $query->orderBy('id', 'DESC')->paginate($pageSize);
            $data = $data->toArray();
            if ($data['data']) {
                $list = $data['data'];
                foreach ($list as $k => $v) {
                    $v['data_str'] = '<pre>' . var_export($v['data'], true) . '</pre>';
                    $list[$k] = $v;
                }
                $data['data'] = $list;
            }
            return $data;
        }
        return view('admin.wxmsg.send', $this->viewData);
    }
}
